package com.hospital.Common;

import android.content.Context;
import android.util.Log;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.hospital.tools.MyApplication;


/**
 * Created by wangjun on 2015/6/10.
 */
public class BLocator {
    private static BLocator mInstance;
    private LocationClient mLocationClient;
    private BDLocationListener mListener;
    private Context mContext;
    private LocationClientOption mClientOption;

    public BLocator(Context con) {
        if (con == null) {
            return;
        }
        mContext = con;
        mClientOption = getLocationOption();
        mLocationClient = new LocationClient(mContext, mClientOption);
    }

    public static BLocator getInstance() {
        if (mInstance == null) {
            mInstance = new BLocator(MyApplication.getInstance());
        }
        return mInstance;
    }

    public void bindService(BDLocationListener locationListener) {
        if (mListener != null) {
            mLocationClient.unRegisterLocationListener(mListener);
        }
        mListener = locationListener;

        mLocationClient.registerLocationListener(mListener);
        mLocationClient.start();
    }

    public boolean requestLocation() {
        if (mLocationClient != null && mLocationClient.isStarted()) {

            mLocationClient.requestLocation();
            Log.d("onTest", "requestLocation");
            return true;
        }
        return false;
    }

    public void unbindService() {
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.stop();
        }
    }

    private LocationClientOption getLocationOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式
        option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度，默认值gcj02
        //option.disableCache(true);
        option.setScanSpan(500);
        option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
        return option;
    }
}
